package com.nft.utils.oss;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.nft.utils.dateFormat.SimpleDateFormatUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

public class OSSUtil {
    private static final String END_POINT = "********";

    private static final String ACCESS_KEY_ID = "********";

    private static final String ACCESS_KEY_SECRET = "********";

    private static final String BUCKET_NAME = "********";

    public static String addImage(String parentPath,MultipartFile brandLogoUrl) {
        // 根据当前日期创建文件夹
        String originalFilename = brandLogoUrl.getOriginalFilename();
        LocalDate localDate = LocalDate.now();
        String objectName = parentPath + "/" + localDate + "/" + originalFilename;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        // 接收返回的url
        String url = "";
        try {
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectName, brandLogoUrl.getInputStream());
            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);
            // 上传文件
            ossClient.putObject(putObjectRequest);
            // 获取文件url
            // url过期时间
            Date date = new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 30 * 12 * 10);
            url = ossClient.generatePresignedUrl(BUCKET_NAME, objectName, date).toString();
//            url = "http://"+BUCKET_NAME+"."+END_POINT+"/"+objectName;
            return url.replace("http","https");
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }

    public static String OSSFileUpload(String fileName) {
        String url = null;
        OSS ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String finalFileName = System.currentTimeMillis() + "" + new SecureRandom().nextInt(0x0400) + suffixName;
        String objectName = sdf.format(SimpleDateFormatUtil.getNowDate()) + "/" + finalFileName;
        File file = new File(fileName);
        ossClient.putObject(BUCKET_NAME, objectName, file);
        url = "http://"+BUCKET_NAME+"."+END_POINT+"/"+objectName;
        ossClient.shutdown();
        return url;
    }
}

